/**
 * 此类专门用来压缩图层面板包
 * @author jw
 * @date 2017-04-01
 */
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var zip = require('gulp-zip');
var rename = require('gulp-rename');
var dateFormat = require('dateformat');
var dataConfig = require('../config/data');
var dealFrame = require('../plugins/dealFrame');
var packageJson = require('../../package.json');

/**
 * @param {String} fileName 要压缩的js文件名称
 * @param {String} filePath 要压缩的js文件路径
 * @param {String} destPath 压缩后的zip包路径
 * @author jw
 * @date 2017-04-01
 */
var zipFrame = function (fileName, filePath, destPath) {
  filePath = filePath || 'frame/';
  destPath = destPath || 'frame';
  var zipName = fileName + '_' + dateFormat(new Date(), 'yyyymmdd') + '.zip';
  return gulp.src([filePath + fileName + '.js'])
    .pipe(uglify())
    .pipe(dealFrame())
    .pipe(rename('frame.js'))
    .pipe(zip(zipName))
    .pipe(gulp.dest(destPath));
};

/**
 * @param {String} fileName 要压缩的js文件名称
 * @param {String} filePath 要压缩的js文件路径
 * @param {String} destPath 压缩后的zip包路径
 * @author jw
 * @date 2017-04-01
 */
var zipAllFrame = function (fileName, filePath, destPath) {
  filePath = filePath || 'frame/';
  destPath = destPath || 'frame';
  return gulp.src([filePath + fileName + '.js'])
    .pipe(uglify())
    .pipe(dealFrame())
    .pipe(rename('frame.js'))
    .pipe(gulp.dest(destPath));
};

/**
 * 打包图层面板包
 */
gulp.task('zipFrame', function () {
  var filePath = dataConfig.dirs.framePath;
  var fileName = dataConfig.dirs.frameName;
  return zipFrame(fileName, filePath, filePath);
});

/**
 * 压缩源码为frame.js
 */
gulp.task('scriptAllFrame', function () {
  var filePath = dataConfig.dirs.frameAllPath;
  var fileName = dataConfig.dirs.frameAllName;
  return zipAllFrame(fileName, filePath, filePath);
});

/**
 * 打包可视化包
 */
gulp.task('zipAllFrame', function () {
  var version = packageJson.version;
  var zipName = 'all-cn-tarsier-node' + '_v' + version + '.zip';
  return gulp.src([dataConfig.dirs.frameAllPath + '\\**\\*', '!' + dataConfig.dirs.frameAllPath + dataConfig.dirs.frameAllName + '.js'],
    {base: dataConfig.dirs.frameAllPath} //jw 2018.01.03 gulp升级后，目录结构有变化，必须加这个参数
  )
    .pipe(zip(zipName))
    .pipe(gulp.dest(dataConfig.dirs.frameAllDestPath));
});

/**
 * 压缩frame.js，并和资源一起打成zip包
 * @author jw
 * @date 2017-11-15
 */
gulp.task('scriptAndzipAllFrame', gulp.series('scriptAllFrame', 'zipAllFrame'));
